package com.lanou.myapplication.PhotoRecyclerFragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapplication.PhotoRecyclerAdapter.FourthphotoRecyclerAdapter;
import com.lanou.myapplication.R;
import com.lanou.myapplication.entity.Photofour;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/24.
 */
public class FourthphotoFragment extends Fragment {
    private RequestQueue queue;
    private FourthphotoRecyclerAdapter fourthphotoRecyclerAdapter;
    private RecyclerView fragmentView;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.activity_photo_recycleview4,null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        queue = Volley.newRequestQueue(getActivity());
        fragmentView =(RecyclerView)getActivity().findViewById(R.id.fragmentView4);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),1);
        fragmentView.setLayoutManager(gridLayoutManager);

        fourthphotoRecyclerAdapter =new FourthphotoRecyclerAdapter(getActivity());
        fragmentView.setAdapter(fourthphotoRecyclerAdapter);
    }
    private void  initData(){
        {
            String path = "http://api.sina.cn/sinago/list.json?channel=hdpic_story&adid=4ad30dabe134695c3b7c3a65977d7e72&wm=b207&from=6042095012&chwm=12050_0001&oldchwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&p=1";
            StringRequest request = new StringRequest(path,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            Gson gson = new Gson();
                            Log.i("lanou", "----" + response);
                            Photofour rList = gson.fromJson(response, Photofour.class);

                            if (rList != null) {
                                List<Photofour.DataEntity.ListEntity> list = rList.getData().getList();
//                                for (Photothree.DataEntity.ListEntity listEntity : list) {
//                                    Log.d("ThirdphotoFragment", listEntity.getPic());
//                                }


                                if (list != null && list.size() > 0) {
                                    fourthphotoRecyclerAdapter.addData(list);

                                }

                            }


                        }


                    },
                    new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();

                        }
                    }){@Override
                       public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;


            } protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
            }
                    ;

            queue.add(request);
        }



    }

}



